package com.aplink.generic.adapter;

import java.util.ArrayList;

import android.widget.Filter;

import com.aplink.generic.pojo.GenericObject;

public class ViewPagerFilter extends Filter {
	private final GalleryPagerAdapter mGalleryPagerAdapter;
	private final ArrayList<String> mSearchFileds;

	public ViewPagerFilter(final ArrayList<String> searchFileds,
			final GalleryPagerAdapter configAdapter) {
		mSearchFileds = searchFileds;
		mGalleryPagerAdapter = configAdapter;
	}

	@Override
	protected FilterResults performFiltering(CharSequence constraint) {
		final FilterResults filterResults = new FilterResults();
		final ArrayList<GenericObject> filteredArrList = new ArrayList<GenericObject>();
		if ((constraint == null) || (constraint.length() == 0)) {
			filterResults.values = mGalleryPagerAdapter.getOriginalObjects();
			filterResults.count = mGalleryPagerAdapter.getOriginalObjects()
					.size();
		} else {
			for (final GenericObject genericObject : mGalleryPagerAdapter
					.getOriginalObjects()) {
				if (genericObject
						.contains(constraint.toString(), mSearchFileds)) {
					filteredArrList.add(genericObject);
				}
			}
			filterResults.count = filteredArrList.size();
			filterResults.values = filteredArrList;
		}
		return filterResults;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void publishResults(CharSequence constraint, FilterResults results) {
		mGalleryPagerAdapter
		.setDisplayObjects((ArrayList<GenericObject>) results.values);
		mGalleryPagerAdapter.notifyDataSetChanged();
	}

}
